﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Medical.Entity;
using Medical.Business;

/// <summary>
/// 页面基础类
/// </summary>
#region 管理后台基础页面类
/// <summary>
/// 管理后台基础页面类
/// </summary>
public class AdminPage : System.Web.UI.Page
{
    /// <summary>
    /// 管理员账号
    /// </summary>
    protected string strAdminUserName = string.Empty;

    /// <summary>
    /// 管理员ID
    /// </summary>
    protected int nAdminUserID;

    /// <summary>
    /// 管理员姓名
    /// </summary>
    protected string strAdminName = string.Empty;

    /// <summary>
    /// 登陆日期
    /// </summary>
    protected string strAdminLoginTime = string.Empty;

    /// <summary>
    /// 管理员权限
    /// </summary>
    protected Int64 nAdminFeatures = 0;

    /// <summary>
    /// 重写预加载
    /// </summary>
    /// <param name="e"></param>
    protected override void OnPreLoad(EventArgs e)
    {
        Response.Cache.SetCacheability(HttpCacheability.NoCache);

        //验证管理员信息
        if (Session["AdminUserID"] != null && Session["AdminUserName"] != null)
        {
            strAdminUserName = Session["AdminUserName"].ToString();
            nAdminUserID = Convert.ToInt32(Session["AdminUserID"]);
            strAdminLoginTime = Session["AdminLoginTime"].ToString();
            if (Session["AdminFeatures"] != null && Session["AdminFeatures"].ToString() != string.Empty)
                nAdminFeatures = Convert.ToInt64(Session["AdminFeatures"]);
            if (Session["AdminName"] != null)
                strAdminName = Session["AdminName"].ToString();
        }
        else
        {
            Response.Write("<script language='javascript'>top.location.href='Default.aspx';</script>");
            Response.End();
        }

        if (nAdminFeatures != 1)
        {
            if (!SupportFeatures(GetNeededFeatures()))
            {
                Response.Write("没有权限！");
                Response.End();
            }
        }
    }

    public virtual long GetNeededFeatures()
    {
        return 0;
    }

    /// <summary>
    /// 判断权限
    /// add by wilhelm
    /// </summary>
    /// <param name="nFeatures"></param>
    /// <returns></returns>
    public bool SupportFeatures(long nFeatures)
    {
        return ((nAdminFeatures & nFeatures) == nFeatures);
    }

    /// <summary>
    /// 出错处理
    /// </summary>
    /// <param name="args"></param>
    protected override void OnError(EventArgs args)
    {
        Exception error = Context.Error;
        if (error != null)
        {
            string strUserName = "";
            if (Session["AdminUserName"] != null) strUserName = Session["AdminUserName"].ToString();
            string strErrorInfo = "\r\n错误描述："
                            + "\r\n管理员：" + strUserName
                            + "\r\n来源:" + error.Source
                            + "\r\n方法：" + error.TargetSite.Name
                            + "\r\n堆栈：" + error.StackTrace
                            + "\r\n信息：" + error.Message
                            + "\r\nUrl：" + Request.Url.AbsoluteUri
                            + "\r\nIP：" + Common.GetVistorsIP();
            //记录日记
            Common.Log(strErrorInfo);
        }
        Response.Redirect("Error.aspx");
    }
}
#endregion

/// <summary>
/// 站点基础页面类
/// </summary>
public class WebPage : System.Web.UI.Page
{
    protected string strWebTitle = string.Empty;

    /// <summary>
    /// 重写预加载
    /// </summary>
    /// <param name="e"></param>
    protected override void OnPreLoad(EventArgs e)
    {
        //页面编码
        HtmlMeta encode = new HtmlMeta();
        encode.HttpEquiv = "Content-Type";
        encode.Content = "text/html; charset=utf-8";
        Page.Header.Controls.AddAt(0, encode);
        //页面关键字和描述
        ConfigInfo Info = Until.GetConfig();
        if (Info != null)
        {
            strWebTitle = Info.Title;
            DataRow[] drList = Until.GetSEO().Select("Type=1");
            if (drList.Length > 0)
                SetPageInfo(drList[0]["PageTitle"].ToString(), drList[0]["PageKeywords"].ToString(), drList[0]["PageDescription"].ToString());
        }
    }

    /// <summary>
    /// 设置页面头部信息
    /// </summary>
    /// <param name="strPageTitle"></param>
    /// <param name="strPageKeyword"></param>
    /// <param name="strDes"></param>
    protected void SetPageInfo(string strPageTitle, string strPageKeyword, string strDes)
    {
        if (!string.IsNullOrEmpty(strPageTitle))
        {
            Page.Title = strPageTitle;
        }
        if (!string.IsNullOrEmpty(strPageKeyword))
        {
            foreach (Control Item in Page.Header.Controls)
            {
                if (Item is HtmlMeta)
                {
                    HtmlMeta meta = (HtmlMeta)Item;
                    if (meta.Name.ToLower() == "keywords")
                    {
                        meta.Content = strPageKeyword;
                        break;
                    }
                }
            }
        }

        if (!string.IsNullOrEmpty(strDes))
        {
            foreach (Control Item in Page.Header.Controls)
            {
                if (Item is HtmlMeta)
                {
                    HtmlMeta meta = (HtmlMeta)Item;
                    if (meta.Name.ToLower() == "description")
                    {
                        meta.Content = strDes;
                        break;
                    }
                }
            }
        }
    }

    /// <summary>
    /// 出错处理
    /// </summary>
    /// <param name="args"></param>
    protected override void OnError(EventArgs args)
    {
        Exception error = Context.Error;
        if (error != null)
        {
            string strErrorInfo = "\r\n错误描述："
                            + "\r\n信息：" + error.Message
                            + "\r\nUrl：" + Request.Url.AbsoluteUri
                            + "\r\nIP：" + Common.GetVistorsIP();
            //记录日记
            Common.Log(strErrorInfo);
        }
        Response.Redirect("/error.html");
    }
}
